﻿using Ec.NETCoreFramework.Common;
using Ec.NETCoreFramework.Common.Core;
using Ec.NETCoreFramework.Common.Core.Helpers;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Ec.NETCoreFramework.Common.Cache
{
    /// <summary>
    /// 单redis 锁
    /// </summary>
    public interface IRedisSingleLock
    {
        /// <summary>
        /// 加锁
        /// </summary>
        /// <param name="key">锁定的key值</param>
        /// <param name="value">锁定的value值</param>
        /// <param name="expiryTime">锁定过期时间，锁区域内的逻辑执行如果超过过期时间，锁将被释放</param>
        /// <param name="waitTime">等待时间,相同的 resource 如果当前的锁被其他线程占用,最多等待时间</param>
        /// <param name="loops">循环次数</param>
        /// <returns></returns>
        bool Lock(string key, string value, TimeSpan expiryTime, TimeSpan waitTime, int loops);
        /// <summary>
        /// 解锁
        /// </summary>
        /// <param name="key">锁定的key值</param>
        /// <param name="value">锁定的value值</param>
        /// <param name="loops">循环次数</param>
        void UnLock(string key, string value, int loops);
        /// <summary>
        /// redis 锁
        /// </summary>
        /// <param name="key">锁定的key值</param>
        /// <param name="expiryTime">锁定过期时间，锁区域内的逻辑执行如果超过过期时间，锁将被释放</param>
        /// <param name="waitTime">等待时间,相同的 resource 如果当前的锁被其他线程占用,最多等待时间</param>
        /// <param name="func">回调函数</param>
        /// <param name="loops">循环次数</param>
        /// <returns>IResponse</returns>
        Task<IResponse> BlockingWork(string key, TimeSpan expiryTime, TimeSpan waitTime, Func<Task<IResponse>> func, int loops = 3);
    }
}
